package com.example.demo.advice;

import com.example.demo.annotation.Decrypt;
import com.example.demo.config.SecretKeyConfig;
import com.example.demo.exception.EncryptRequestException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.MethodParameter;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.reactive.BindingContext;
import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
import org.springframework.web.reactive.result.method.annotation.RequestBodyMethodArgumentResolver;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Objects;

/**
 * Author:Bobby
 * DateTime:2019/4/9
 **/
public class EncryptRequestBodyAdvice extends RequestBodyMethodArgumentResolver {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    private boolean encrypt;
    private Decrypt decryptAnnotation;

    @Autowired
    ServerCodecConfigurer serverCodecConfigurer;
    @Autowired
    RequestedContentTypeResolver requestedContentTypeResolver;

    @Autowired
    private SecretKeyConfig secretKeyConfig;
    @Autowired
    ReactiveAdapterRegistry reactiveAdapterRegistry;


    public EncryptRequestBodyAdvice(List<HttpMessageReader<?>> readers, ReactiveAdapterRegistry registry) {
        super(readers, registry);
    }

    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        Method method = methodParameter.getMethod();
        if (Objects.isNull(method)) {
            encrypt = false;
            return false;
        }
        if (method.isAnnotationPresent(Decrypt.class) && secretKeyConfig.isOpen()) {
            encrypt = true;
            decryptAnnotation = methodParameter.getMethodAnnotation(Decrypt.class);
            return true;
        }
        // 此处如果按照原逻辑直接返回encrypt, 会造成一次修改为true之后, 后续请求都会变成true, 在不支持时, 需要做修正
        encrypt = false;
        return false;
    }

    @Override
    public Mono<Object> resolveArgument(MethodParameter methodParameter, BindingContext bindingContext, ServerWebExchange serverWebExchange) {
        if (encrypt) {
            try {
                return Mono.just(new DecryptHttpInputMessage(serverWebExchange, secretKeyConfig, decryptAnnotation));
            } catch (EncryptRequestException e) {
                throw e;
            } catch (Exception e) {
                log.error("Decryption failed", e);
            }
        }
        RequestBody ann = methodParameter.getParameterAnnotation(RequestBody.class);
        return readBody(methodParameter, ann.required(), bindingContext, serverWebExchange);
    }
}
